﻿using BCL.Tool.Extend;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Xml.Serialization;
namespace DevicesWPF.Logic
{
    public class DevicesLogic
    {
        private static Dictionary<string, DevAutoConfigInfo> DevicesTests;

        static DevicesLogic()
        {
            DevicesTests = new Dictionary<string, DevAutoConfigInfo>();
        }

        public static void Save(string infoCode, DevAutoConfigInfo test)
        {
            DevicesView.Logic.Tool.ObjectSaveToXML(test, $"Config\\{infoCode}.xml");
        }

        public static DevAutoConfigInfo GetDevicesTest(string infoCode)
        {
            if (DevicesTests.ContainsKey(infoCode))
                return DevicesTests[infoCode];
            DevAutoConfigInfo test = DevicesView.Logic.Tool.GetObjectByXML<DevAutoConfigInfo>($"Config\\{infoCode}.xml");
            if (test != null)
                DevicesTests.Add(infoCode, test);
            return test;
        }

        private static DevAutoConfigInfo GetDevicesTestDefault(string infoCode)
        {
            DevAutoConfigInfo dacfi = new DevAutoConfigInfo();
            if (infoCode == "")
            {
                dacfi.Tests = new ObservableCollection<DevicesTest>();
                dacfi.Code = infoCode;
                dacfi.Selected = true;
                dacfi.Tests = new ObservableCollection<DevicesTest>();

                DevicesTest test = new DevicesTest {Name= "肾功能组合2(Kidney2)" };
                test.Items = new ObservableCollection<DevicesTestItem>();
                test.Items.Add(new DevicesTestItem{Code= "UN" });
                test.Items.Add(new DevicesTestItem{Code= "UA" });
                test.Items.Add(new DevicesTestItem{Code= "TP" });
                test.Items.Add(new DevicesTestItem{Code= "ALB" });
                test.Items.Add(new DevicesTestItem{Code= "CRE2" });
                test.Items.Add(new DevicesTestItem{Code= "UN/CRE2" });
                dacfi.Tests.Add(test);

                DevicesTest test1 = new DevicesTest { Name = "肝功能组合(Liver)" };
                test1.Items = new ObservableCollection<DevicesTestItem>();
                test1.Items.Add(new DevicesTestItem{Code= "ALB" });
                test1.Items.Add(new DevicesTestItem{Code= "TP" });
                test1.Items.Add(new DevicesTestItem{Code= "GGT" });
                test1.Items.Add(new DevicesTestItem{Code= "ALP" });
                test1.Items.Add(new DevicesTestItem{Code= "AST" });
                test1.Items.Add(new DevicesTestItem{Code= "ALT"});
                dacfi.Tests.Add(test1);

                DevicesTest test2 = new DevicesTest { Name = "常规组合(Metabolic2)" };
                test2.Items = new ObservableCollection<DevicesTestItem>();
                test.Items.Add(new DevicesTestItem{Code= "HDL" });
                test.Items.Add(new DevicesTestItem{Code= "TC" });
                test.Items.Add(new DevicesTestItem{Code= "TG" });
                test.Items.Add(new DevicesTestItem{Code= "UA" });
                test.Items.Add(new DevicesTestItem{Code= "GLU" });
                test.Items.Add(new DevicesTestItem{ Code = "CRE2"});
                dacfi.Tests.Add(test2);

                DevicesTest test3 = new DevicesTest { Name = "急诊组合(Stat2)" };
                test3.Items = new ObservableCollection<DevicesTestItem>();
                test3.Items.Add(new DevicesTestItem{ Code="TP" });
                test3.Items.Add(new DevicesTestItem{ Code="AST" });
                test3.Items.Add(new DevicesTestItem{ Code="CK" });
                test3.Items.Add(new DevicesTestItem{ Code="LD" });
                test3.Items.Add(new DevicesTestItem{ Code="CRE2" });
                dacfi.Tests.Add(test3);

                DevicesTest test4 = new DevicesTest { Name = "综合组合A2(Total-A2)" };
                test4.Items = new ObservableCollection<DevicesTestItem>();
                    test4.Items.Add(new DevicesTestItem{Code= "TC" });
                test4.Items.Add(new DevicesTestItem{Code= "GLU" });
                test4.Items.Add(new DevicesTestItem{Code= "UN" });
                test4.Items.Add(new DevicesTestItem{Code= "AST" });
                test4.Items.Add(new DevicesTestItem{Code= "ALT" });
                test4.Items.Add(new DevicesTestItem{Code= "CRE2" });
                test4.Items.Add(new DevicesTestItem{Code= "UN/CRE2" });
                dacfi.Tests.Add(test4);

                DevicesTest test5 = new DevicesTest { Name = "综合组合B(Total-B)" };
                test5.Items = new ObservableCollection<DevicesTestItem>();
                test5.Items.Add(new DevicesTestItem{Code= "HDL" });
                test5.Items.Add(new DevicesTestItem{Code= "TC" });
                test5.Items.Add(new DevicesTestItem{Code= "TG" });
                test5.Items.Add(new DevicesTestItem{Code= "GGT" });
                test5.Items.Add(new DevicesTestItem{Code= "AST" });
                test5.Items.Add(new DevicesTestItem{Code= "ALT" });
                dacfi.Tests.Add(test5);


                DevicesTest test6 = new DevicesTest { Name = "术前/基础套组V2(Pannel-V2)" };
                test6.Items = new ObservableCollection<DevicesTestItem>();
                //UN、GLU、ALP、 TP、ALT、CRE、BUN / CRE
                dacfi.Tests.Add(test6);


                DevicesTest test7 = new DevicesTest { Name = "肝功能组合(Liver)" };
                test7.Items = new ObservableCollection<DevicesTestItem>();
                dacfi.Tests.Add(test7);


                DevicesTest test8 = new DevicesTest { Name = "肝功能组合(Liver)" };
                test8.Items = new ObservableCollection<DevicesTestItem>();
                dacfi.Tests.Add(test8);
            }
            return dacfi;
        }
        public static ObservableCollection<DevicesTest> GetDevicesTests()
        {
            if (Devices.DevicesCollection.Devices == null)
                return null;
            ObservableCollection<DevicesTest> tests = new ObservableCollection<DevicesTest>();
            foreach (var item in Devices.DevicesCollection.Devices)
            {
                DevAutoConfigInfo dacf = GetDevicesTest(item.Info.Code);
                if (dacf.Selected && dacf.Tests!=null && dacf.Tests.Count>0)
                    foreach (var test in dacf.Tests)
                        tests.Add(test);
            }
            if (tests.Count == 0)
                return null;
            return tests;
        }
    }



    [Serializable]
    public class DevAutoConfigInfo : INotifyPropertyChanged
    {
        private string code;
        private bool selected;
        private string name;

        public string Name {
            get => name;
            set
            {
                name = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }
        public string Code
        {
            get => code;
            set
            {
                code = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Code"));
            }
        }
        public bool Selected
        {
            get => selected;
            set
            {
                selected = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Selected"));
            }
        }
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<DevicesTest> Tests { get; set; }
    }

    /// <summary>
    /// 检查
    /// </summary>
    [Serializable]
    public class DevicesTest : INotifyPropertyChanged
    {
        public static BitmapImage addImage;
        public static BitmapImage deleteImage;
        private string name;

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        public string Code { get; set; }

        public string Name
        {
            get => name;
            set
            {
                name = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }

        public ObservableCollection<DevicesTestItem> Items { get; set; }
        [XmlIgnore]
        public BitmapImage AddImage { get { return addImage; } set { } }
        [XmlIgnore]
        public BitmapImage DeleteImage { get { return deleteImage; } set { } }

        static DevicesTest()
        {
            addImage = File.ReadAllBytes("image/add.png").ToBitmap();
            addImage = File.ReadAllBytes("image/icon_d3.png").ToBitmap();
        }

        public DevicesTest()
        {
            Code = Guid.NewGuid().ToString();
        }
    }

    /// <summary>
    /// 检查项
    /// </summary>
    [Serializable]
    public class DevicesTestItem : INotifyPropertyChanged
    {
        private string code;
        private string name;
        public string Code
        {
            get => code;
            set
            {
                code = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }

        public string Name
        {
            get => name;
            set
            {
                name = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }

        [XmlIgnore]
        public BitmapImage AddImage { get { return DevicesTest.addImage; } set { } }
        [XmlIgnore]
        public BitmapImage DeleteImage { get { return DevicesTest.deleteImage; } set { } }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;
    }




}
